package gov.dhs.cis.soa.xslt;

import gov.dhs.cis.soa.data.Description;
import gov.dhs.cis.soa.data.Service;
import gov.dhs.cis.soa.data.ServiceOperation;

import java.util.ArrayList;

import org.apache.log4j.Logger;

public class ServicesUtils {
	static final Logger loger = Logger.getLogger(ServicesUtils.class);
	static final String lineSep = (String) System.getProperties().get("line.separator");

	public static void initialize(Service servObj) {
		servObj.name = "";
		servObj.descText = "";
		servObj.stereotype = "";
		servObj.wipt = "";
		servObj.release = "";
		servObj.maturity = "-1";
		servObj.applications = "";
		servObj.extsystems = "";
		servObj.otherservices = "";
		servObj.l3busprocname = "";
		servObj.servDefID = "";
		servObj.agent = "";
		servObj.exposure = "";
		servObj.realization = "";
		servObj.inthostcfg = "";
		servObj.intesbendpt = "";
		servObj.designAspects = "";

		servObj.opList = new ArrayList<ServiceOperation>();
	}

	public static void parseName(Service servObj, String name) {
		servObj.name = name;
		if(loger.isDebugEnabled())
			loger.debug("Extracting Service : " + servObj.name);
	}
	
	public static void parseStereoType(Service servObj, String stereo) {
		servObj.stereotype = stereo;
	}
	public static void parseAttribute(Service servObj, String attrib) {
		if(attrib == null || attrib.trim().length() == 0)
			return;
		String toks[] = attrib.split("=");
		if(toks.length != 2)
			return;
		String name = toks[0].trim();
		String value = toks[1].trim();
		
		if("Maturity".equalsIgnoreCase(name))
			servObj.maturity = value;
		else if("Explosure".equalsIgnoreCase(name))
			servObj.exposure = value;
		else if("Realization".equalsIgnoreCase(name))
			servObj.realization = value;
		else if("Exposes Capability".equalsIgnoreCase(name))
			servObj.extsystems = value;
	}
	
	public static void parseDescription(Service servObj, String desc, String delim) {
		//System.out.println("DESC:" + desc + ", DELIM:" + delim);
		if(desc == null || desc.trim().length() == 0)
			return;
		servObj.desc = new Description();
		servObj.desc.descTxt = desc;
		String[] dToks = desc.split("\\n");
		String toks[] = null;
		String name = null;
		String value = null;
		for(String tok : dToks) {
			toks = tok.split("=");
			if(toks.length != 2)
				continue;
			name = toks[0].trim();
			value = toks[1].trim();
			if("Description".equalsIgnoreCase(name))
				servObj.descText = value;
			else if("Design Aspects".equalsIgnoreCase(name))
				servObj.designAspects = value;
			else if("Business Process".equalsIgnoreCase(name))
				servObj.l3busprocname = value;
		}
		//return ret;
	}

	public static void parseAssociation(Service servObj, String association) {
		String[] toks = association.split("_");
		String value = toks[toks.length-1].trim();
		if(value.length() == 1)
			servObj.release = value;
		else
			servObj.wipt = value;
	}
	
	public static void parseOperation(Service servObj, String opName, String direction, String dataName) {
		if(opName == null || opName.trim().length() == 0)
			return;
		ServiceOperation sop = ServicesUtils.findOperation(servObj, opName);
		if(sop == null) {
			sop = new ServiceOperation();
			sop.name = opName.trim();
			sop.dataIn = "";
			sop.dataOut = "";
			servObj.opList.add(sop);
		}
		if("in".equalsIgnoreCase(direction))
			sop.dataIn = dataName;
		else
			sop.dataOut = dataName;
		if(loger.isDebugEnabled())
			loger.debug("Extracted Service Operation : " + sop.name);
	}

	private static ServiceOperation findOperation(Service servObj, String opName) {
		for(ServiceOperation op : servObj.opList) {
			if(opName.equalsIgnoreCase(op.name)) {
				return op;
			}
		}
		return null;
	}

	public static void parseDependency(Service servObj, String name, String supplier) {
		//Invokes Interface - is not done
		if ("Exposes Capability".equalsIgnoreCase(name)) {
			if(servObj.applications.length() > 0)
				servObj.applications += ", ";
			servObj.applications += supplier;
		} else if ("Invokes Service".equalsIgnoreCase(name)) {
			if(servObj.otherservices.length() > 0)
				servObj.otherservices += ", ";
			servObj.otherservices += supplier;
		} else if ("Hosts Service".equalsIgnoreCase(name)) {
			if(servObj.inthostcfg.length() > 0)
				servObj.inthostcfg += ", ";
			servObj.inthostcfg += supplier;
		} else if ("Accessed Via".equalsIgnoreCase(name)) {
			if(servObj.intesbendpt.length() > 0)
				servObj.intesbendpt += ", ";
			servObj.intesbendpt += supplier;
		} else if ("Implements Service Definition".equalsIgnoreCase(name)) {
			if(servObj.servDefID.length() > 0)
				servObj.servDefID += ", ";
			servObj.servDefID += supplier;
		} else if ("Implemented by Agent".equalsIgnoreCase(name)) {
			if(servObj.agent.length() > 0)
				servObj.agent += ", ";
			servObj.agent += supplier;
		}
		//System.out.println(servObj.name + ": " + name + " = " + supplier);
	}
	
	public static String serviceToString(Service servObj, String delim) {
		StringBuffer ret = new StringBuffer();
		
		ret.append(servObj.name);
		ret.append(delim);
		ret.append(servObj.descText);
		ret.append(delim);
		ret.append(servObj.stereotype);
		ret.append(delim);
		ret.append(servObj.wipt);
		ret.append(delim);
		ret.append(servObj.release);
		ret.append(delim);
		ret.append(servObj.maturity);
		ret.append(delim);
		ret.append(servObj.applications);
		ret.append(delim);
		ret.append(servObj.extsystems);
		ret.append(delim);
		ret.append(servObj.otherservices);
		ret.append(delim);
		ret.append(delim);
		ret.append(servObj.l3busprocname);
		ret.append(delim);
		ret.append(servObj.servDefID);
		ret.append(delim);
		ret.append(servObj.agent);
		ret.append(delim);
		ret.append(servObj.exposure);
		ret.append(delim);
		ret.append(servObj.realization);
		ret.append(delim);
		ret.append(servObj.inthostcfg);
		ret.append(delim);
		ret.append(servObj.intesbendpt);
		ret.append(delim);
		ret.append(servObj.designAspects);
		//ret.append(delim);
		return ret.toString();
	}
	
	public static String serviceOpsToString(Service servObj, String delim) {
		StringBuffer ret = new StringBuffer();
		
		if(servObj != null) {
			for(ServiceOperation op : servObj.opList) {
				ret.append(lineSep);
				ret.append(servObj.name);
				ret.append(delim);
				ret.append(op.name);
				ret.append(delim);
				ret.append(op.dataIn);
				ret.append(delim);
				ret.append(op.dataOut);
			}
		}
		return ret.toString();
	}
	
	public static String getNameForWBM(Service servObj, String delim) {
		StringBuffer ret = new StringBuffer();
		
		if(servObj != null) {
			ret.append(servObj.wipt);
			ret.append("##");
			ret.append(servObj.name);
		}
		return ret.toString();
	}

	public static String getDescForWBM(Service servObj) {
		StringBuffer ret = new StringBuffer();
		
		if(servObj != null) {
			ret.append(servObj.desc.descTxt);
			ret.append(lineSep);
			ret.append("Invokes Services = " + servObj.otherservices);
		}
		return ret.toString();
	}

	public static String getExtsystem(Service servObj) {
		String ret = "";
		//name="Srvc32Ty##External"
		if(servObj != null) {
			ret = servObj.extsystems;
		}
		if(ret == null || ret.trim().length() == 0)
			ret = "NONE";
		return ret;
	}
	
	public static String getRelease(Service servObj) {
		String ret = "";
		if(servObj != null) {
			ret = servObj.release;
		}
		return ret;
	}
}
